package com.itbupt.controller;

import com.itbupt.dto.LoginFormDTO;
import com.itbupt.dto.Result;
import com.itbupt.dto.UserDTO;
import com.itbupt.entity.User;
import com.itbupt.service.IUserInfoService;
import com.itbupt.service.IUserService;
import com.itbupt.utils.MailUtils;
import com.itbupt.utils.RegexUtils;
import com.itbupt.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/5/2 23:29
 * ClassName: UserController
 * Description:
 */

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;



    /**
     * 发送短信验证码---这里使用邮箱验证码代替
     * @param phone
     * @param session
     * @return
     */
    @PostMapping("code")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session) throws MessagingException {
        // TODO 发送短信验证码并保存验证码
        return userService.sendCode(phone, session);
    }


    /**
     * 用户登陆
     * @param loginForm 登陆表单
     * @param session   会话
     * @return        登陆结果
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
        // TODO 实现登录功能
        return userService.login(loginForm, session);
    }


    @GetMapping("/me")
    public Result me(){
        //  获取当前登录的用户并返回
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }


}
